
package com.springcloud.zuul.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springcloud.zuul.common.utlis.JsonResult;
import com.springcloud.zuul.common.utlis.R;
import com.springcloud.zuul.entity.SysUserTokenEntity;
import com.springcloud.zuul.mapper.SysUserTokenDao;
import com.springcloud.zuul.oauth2.TokenGenerator;
import com.springcloud.zuul.service.ShiroService;
import com.springcloud.zuul.service.SysConfigService;
import com.springcloud.zuul.service.SysUserTokenService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service("sysUserTokenService")
public class SysUserTokenServiceImpl extends ServiceImpl<SysUserTokenDao, SysUserTokenEntity> implements SysUserTokenService {

    @Autowired
    SysConfigService sysConfigService;

    @Autowired
	SysUserTokenDao sysUserTokenDao;

    @Autowired
    private ShiroService shiroService;

    @Override
    public JsonResult createToken(String userId) {
        //生成一个token
        String token = TokenGenerator.generateValue();

        //当前时间
        Date now = new Date();
        //过期时间
        Date expireTime = new Date(now.getTime() + Integer.parseInt(sysConfigService.getValue("tokenExpire")) * 1000);

        //开启单点登录
        if (Boolean.parseBoolean(sysConfigService.getValue("singlePoint"))) {
            //1.删除已有当前用户所有的token，然后创建最新的token
			sysUserTokenDao.removeByUserId(userId);
			//2.然后创建新的token资料
			SysUserTokenEntity tokenEntity=new SysUserTokenEntity();
			tokenEntity = new SysUserTokenEntity();
			tokenEntity.setUserId(userId);
			tokenEntity.setToken(token);
			tokenEntity.setExpireTime(expireTime);
			//保存token
			this.save(tokenEntity);
        }else{
        	//无须管之前的token，从新创建一个新的
			SysUserTokenEntity tokenEntity=sysUserTokenDao.queryByUserId(userId);
			tokenEntity = new SysUserTokenEntity();
			tokenEntity.setUserId(userId);
			tokenEntity.setToken(token);
			tokenEntity.setExpireTime(expireTime);

			//保存token
			this.save(tokenEntity);
		}

        //判断是否生成过token
//        SysUserTokenEntity tokenEntity = this.getById(userId);
//        if (tokenEntity == null) {
//            tokenEntity = new SysUserTokenEntity();
//            tokenEntity.setUserId(userId);
//            tokenEntity.setToken(token);
//            tokenEntity.setExpireTime(expireTime);
//
//            //保存token
//            this.save(tokenEntity);
//        } else {
//            tokenEntity.setToken(token);
//            tokenEntity.setExpireTime(expireTime);
//            //更新token
//            this.updateById(tokenEntity);
//        }

        Map<String,Object>result=new HashMap();
        result.put("token", token);
        result.put("expire", sysConfigService.getValue("tokenExpire"));
        return JsonResult.success(result);
    }

    @Override
    public void logout(String loginToken) {
        //生成一个新的token
        String token = TokenGenerator.generateValue();
        //根据accessToken，查询用户信息
        SysUserTokenEntity tokenEntity = shiroService.queryByToken(loginToken);
        //修改token
        tokenEntity.setToken(token);

        sysUserTokenDao.updateById(tokenEntity.getGuId(),tokenEntity.getToken());
    }
}
